package gov.pbc.nn.kjgl.train.dao;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.entity.TrainingPlan;
import gov.pbc.nn.kjgl.entity.TrainingUser;
import gov.pbc.nn.uflo.entity.Opinion;

@Repository
public class TrainPlanSHDao extends AbstractHibernateDao {

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	/**
	 * 根据会议审核员和部门处长获取相应的审核会议
	 * 
	 * @param page
	 */
	public void getAll(Page<TrainingPlan> page, Criteria criteria) {
		String username = ContextHolder.getLoginUserName();
		if (SecurityUtils.containsRole(username, Constants.ROLE_PXSHY)) {
			DetachedCriteria query = this.buildDetachedCriteria(criteria, TrainingPlan.class);
			query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
			query.add(Restrictions.eq("pxshy", ContextHolder.getLoginUserName()));
			query.add(Restrictions.eq("state", 5));
			query.addOrder(Order.desc("updateDate"));
			this.pagingQuery(page, query);
		}
		if (page.getEntityCount() == 0) {
			// if (SecurityUtils.containsRole(username, Constants.ROLE_CZ)) {
			DetachedCriteria query = this.buildDetachedCriteria(criteria, TrainingPlan.class);
			query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
			// query.add(Restrictions.like("applyDeptId",
			// ContextHolder.getLoginUser().getDepts().get(0).getId(),MatchMode.START));
			query.add(Restrictions.eq("state", 12));
			query.add(Restrictions.eq("czUsername", ContextHolder.getLoginUserName()));
			query.addOrder(Order.desc("updateDate"));
			this.pagingQuery(page, query);
			// }
		}
	}

	/**
	 * 如果 type=1 则是通过，那么保存意见，修改状态10 否则就是不通过，那么保存意见，修改状态11
	 * 
	 * @param opinion
	 * @param trainPlan
	 * @param type
	 */
	public void updateState(String opinion, TrainingPlan trainPlan, int cldsh, String type, String czName,
			String czUsername) {
		if ("ok".equals(type)) {
			String deptId = ContextHolder.getLoginUser().getDepts().get(0).getId();
			List<IUser> ls = SecurityUtils.getUserByRoleForDept(deptId, Constants.ROLE_CZ);
			for (IUser u : ls) {
				if (ContextHolder.getLoginUserName().equals(u.getUsername())) {
					cldsh = 3;
				}
			}
			if (cldsh == 1) {// 勾选处领导，同意
				trainPlan.setState(12);
				trainPlan.setCldsh(cldsh);
				trainPlan.setCzName(czName);
				trainPlan.setCzUsername(czUsername);
			} else if (cldsh == 3) {// 审核员是处领导
				trainPlan.setState(10);
				trainPlan.setCldsh(1);
				trainPlan.setCzName(ContextHolder.getLoginUser().getCname());
				trainPlan.setCzUsername(ContextHolder.getLoginUserName());
			} else {// 不勾选处领导，同意
				trainPlan.setState(10);
				trainPlan.setCldsh(cldsh);
			}
			this.update(trainPlan);
			final Opinion o = new Opinion();
			o.setOpinion(opinion);
			o.setBusKey(trainPlan.getId());
			o.setBusTable("T_TRAINING_PLAN");// 业务表表名称
			o.setOpinion(opinion);
			o.setUsername(ContextHolder.getLoginUserName());
			o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
			o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
			o.setCname(ContextHolder.getLoginUser().getCname());
			o.setCreateUser(ContextHolder.getLoginUserName());
			o.setCreateDate(new Date());
			this.coreHibernateDao.save(o);
		} else if ("no".equals(type)) {
			trainPlan.setState(11);
			trainPlan.setCldsh(0);
			this.update(trainPlan);
			final Opinion o = new Opinion();
			o.setOpinion(opinion);
			o.setBusKey(trainPlan.getId());
			o.setBusTable("T_TRAINING_PLAN");// 业务表表名称
			o.setOpinion(opinion);
			o.setUsername(ContextHolder.getLoginUserName());
			o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
			o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
			o.setCname(ContextHolder.getLoginUser().getCname());
			o.setCreateUser(ContextHolder.getLoginUserName());
			o.setCreateDate(new Date());
			this.coreHibernateDao.save(o);
		}
	}

}
